package com.telecom.at.front;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import com.telecom.at.db.dac.Dac;
import com.telecom.at.db.dac.Paging;
import com.telecom.at.db.model.AvgFlow;
import com.telecom.at.db.model.BWList;
import com.telecom.at.db.model.BasCrm3AData;
import com.telecom.at.db.model.NumOfAccount;
import com.telecom.at.db.model.SameMac;
import com.telecom.at.db.model.User;

@WebServlet("/front/ListAccountOfSameMac")
public class ListAccountOfSameMac extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doPost(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String basIp = request.getParameter("basIp");
		String leftMac = request.getParameter("leftMac");
		
		String account = request.getParameter("account");
		
		if (account == null)
			account = "";

		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr == null)
			pageNoStr = "0";
		int pageNo = Integer.parseInt(pageNoStr);

		String pageSizeStr = request.getParameter("pageSize");
		if (pageSizeStr == null)
			pageSizeStr = "20";
		int pageSize = Integer.parseInt(pageSizeStr);

		if (request.getParameter("lastPage") != null)
			pageNo = Integer.MAX_VALUE;
		if (request.getParameter("firstPage") != null)
			pageNo = 0;
		if (request.getParameter("previousPage") != null)
			pageNo--;
		if (request.getParameter("nextPage") != null)
			pageNo++;
		
		
		
		List<BasCrm3AData> basCrm3AList = null;
		//List<NumOfAccount> numList = null;
        SameMac sameMac = (SameMac) request.getSession().getAttribute("sameMac");
		User curUser = (User) request.getSession().getAttribute("curUser");
		
			basCrm3AList = Dac.getInstance().getAccountOfSameMac(basIp,leftMac);
			//numList = Dac.getInstance().numOfAccount();
		//System.out.println(basCrm3AList);
		List<BWList>  bWList =Dac.getInstance().allBWList();
		Set<String> accountList=new HashSet<String>();
	       for(BWList bWList2:bWList)
	    	   accountList.add(bWList2.getAccount());
		//List<NumOfAccount> numList = null;
		
		List<BasCrm3AData> flowList2 =new ArrayList<BasCrm3AData>();
		for(BasCrm3AData avgFlow:basCrm3AList) {
			if(!accountList.contains(avgFlow.getAccount())){
				
				flowList2.add(avgFlow);
			}
		}
		//else
			//flowList = Dac.getInstance().getAvgFlowOfUser(curUser.getCity());
		//List<AvgFlow> flowList = Dac.getInstance().findAvgFlowByAccount(account);
		//System.out.println(macList);
		
		Paging<BasCrm3AData> paging = new Paging<BasCrm3AData>(pageSize, pageNo, flowList2);
		request.setAttribute("basIp", basIp);
		request.setAttribute("leftMac", leftMac);
		request.setAttribute("paging", paging);
		request.setAttribute("account", account);
		request.setAttribute("basCrm3AList", basCrm3AList);
		//request.setAttribute("numList", numList);			
		request.getRequestDispatcher("/WEB-INF/jsp/front/ListAccoutOfSameMac.jsp").forward(request, response);		
	}

}
